﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Layout;

namespace DnPakExplorer.Controls {
	// This class implements a custom ToolStripRenderer for the 
	// GridStrip control. It customizes three aspects of the 
	// GridStrip control's appearance: GridStrip border, 
	// ToolStripButton border, and ToolStripButton image.
	internal class ContextMenuStripRenderer : ToolStripRenderer {
		// The style of the empty cell's text.
		private static StringFormat style = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Near };

		// The thickness (width or height) of a 
		// ToolStripButton control's border.
		static int borderThickness = 2;

		// The main bitmap that is the source for the 
		// subimagesthat are assigned to individual 
		// ToolStripButton controls.
		private Bitmap bmp = null;

		// The brush that paints the background of 
		// the GridStrip control.

		// This method initializes the GridStripRenderer by
		// creating the image that is used as the source for
		// the individual button images.
		protected override void Initialize(ToolStrip ts) {
			base.Initialize(ts);
			this.InitializeBitmap(ts);
		}

		// This method initializes an individual ToolStripButton
		// control. It copies a subimage from the GridStripRenderer's
		// main image, according to the position and size of 
		// the ToolStripButton.
		protected override void InitializeItem(ToolStripItem item) {
			base.InitializeItem(item);
		}

		// This utility method creates the main image that
		// is the source for the subimages of the individual 
		// ToolStripButton controls.
		private void InitializeBitmap(ToolStrip toolStrip) {
			// Create the main bitmap, into which the image is drawn.
			this.bmp = new Bitmap(
				toolStrip.Size.Width,
				toolStrip.Size.Height);

			// Draw a fancy pattern. This could be any image or drawing.
			using (Graphics g = Graphics.FromImage(bmp)) {
				// Draw smoothed lines.
				g.SmoothingMode = SmoothingMode.AntiAlias;

				// Draw the image. In this case, it is 
				// a number of concentric ellipses. 
				for (int i = 0; i < toolStrip.Size.Width; i += 8) {
					g.DrawEllipse(Pens.Blue, 0, 0, i, i);
				}
			}
		}

		// This method draws a border around the GridStrip control.
		protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e) {
			if (e.ToolStrip.GripDisplayStyle == ToolStripGripDisplayStyle.Horizontal)
				e.Graphics.DrawRectangle(Pens.Black, e.AffectedBounds);
			else {
				base.OnRenderToolStripBorder(e);
			}
		}

		// This method renders the GridStrip control's background.
		protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e) {
			if (e.ToolStrip.GripDisplayStyle == ToolStripGripDisplayStyle.Horizontal){
				e.Graphics.FillRectangle( new SolidBrush(Color.FromArgb(16, 16, 16)), e.AffectedBounds);
			} else {
				base.OnRenderToolStripBackground(e);
			}
		}

		// This method draws a border around the button's image. If the background
		// to be rendered belongs to the empty cell, a string is drawn. Otherwise,
		// a border is drawn at the edges of the button.
		protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e) {
			if (e.ToolStrip.GripDisplayStyle == ToolStripGripDisplayStyle.Horizontal) {
				Graphics g = e.Graphics;
				ToolStripButton gsb = e.Item as ToolStripButton;

				// Calculate the rectangle around which the border is painted.
				Rectangle imageRectangle = new Rectangle(
					borderThickness,
					borderThickness,
					gsb.Width - 2 * borderThickness,
					gsb.Height - 2 * borderThickness);

				g.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 255, 0)), imageRectangle);
			} else {
				base.OnRenderButtonBackground(e);
			}
		}

		protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs e) {
			Graphics g = e.Graphics;
			ToolStripButton gsb = e.Item as ToolStripButton;

			// Calculate the rectangle around which the border is painted.
			Rectangle imageRectangle = new Rectangle(
				borderThickness,
				borderThickness,
				gsb.Width - 2 * borderThickness,
				gsb.Height - 2 * borderThickness);

			g.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), imageRectangle);
		}

		protected override void OnRenderGrip(ToolStripGripRenderEventArgs e) {
			Graphics g = e.Graphics;

			// Calculate the rectangle around which the border is painted.
			g.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 255)), e.AffectedBounds);
		}

		protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) {
			Graphics g = e.Graphics;
			
			if (e.ToolStrip.GripDisplayStyle == ToolStripGripDisplayStyle.Horizontal) {
				Rectangle rect = GetItemRect(e.Item);
				if (e.Item.Selected) {
					g.FillRectangle(new SolidBrush(Color.Crimson), rect);
				} else {
					g.FillRectangle(new SolidBrush(Color.FromArgb(80, 80, 80)), rect);
				}
			} else {
				if (!e.Item.Pressed) {
					if (e.Item.Selected) {
						g.FillRectangle(new SolidBrush(Color.Crimson), e.Item.ContentRectangle);
					} else {
						g.FillRectangle(new SolidBrush(e.ToolStrip.BackColor), e.Item.ContentRectangle);
					}
				} else {
					g.FillRectangle(new SolidBrush(Color.Black), e.Item.ContentRectangle);
				}
				
			}
			
		}

		protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) {
			Graphics g = e.Graphics;
			g.DrawString(e.Text, e.TextFont, new SolidBrush(Color.WhiteSmoke), ToRectangleF(e.TextRectangle), style);
		}

		private Rectangle GetItemRect(ToolStripItem item) {
			return new Rectangle(1, item.ContentRectangle.Y-1, item.ContentRectangle.Width + 3, item.ContentRectangle.Height+1);
		}

		private RectangleF ToRectangleF(Rectangle rect) {
			return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
		}

		protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e) {
			base.OnRenderItemImage(e);
		}
	}


	


}